<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/* * ***** BEGIN LICENSE BLOCK *****
<span class='line'>  2</span>  *
<span class='line'>  3</span>  * The contents of this file are subject to the BitTorrent Open Source License
<span class='line'>  4</span>  * Version 1.0 (the License).  You may not copy or use this file, in either
<span class='line'>  5</span>  * source code or executable form, except in compliance with the License.  You
<span class='line'>  6</span>  * may obtain a copy of the License at http://www.bittorrent.com/license/.
<span class='line'>  7</span>  *
<span class='line'>  8</span>  * Software distributed under the License is distributed on an AS IS basis,
<span class='line'>  9</span>  * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
<span class='line'> 10</span>  * for the specific language governing rights and limitations under the
<span class='line'> 11</span>  * License.
<span class='line'> 12</span>  *
<span class='line'> 13</span>  * Original Written by Petru Paler
<span class='line'> 14</span>  * Translated to Javascript by Matthew Mead Armsby
<span class='line'> 15</span>  * Version: 2009-02-27
<span class='line'> 16</span>  *
<span class='line'> 17</span>  * **** END LICENSE BLOCK ***** */</span><span class="WHIT">
<span class='line'> 18</span> 
<span class='line'> 19</span> </span><span class="COMM">// Grab XPCOMUtils. This limits this component to Firefox 3.</span><span class="WHIT">
<span class='line'> 20</span> </span><span class="COMM">// See https://developer.mozilla.org/en/How_to_Build_an_XPCOM_Component_in_Javascript</span><span class="WHIT">
<span class='line'> 21</span> </span><span class="NAME">Components.utils.import</span><span class="PUNC">(</span><span class="STRN">"resource://gre/modules/XPCOMUtils.jsm"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 22</span> 
<span class='line'> 23</span> </span><span class="COMM">/**
<span class='line'> 24</span>  * Decodes a bencoded int to an actual int
<span class='line'> 25</span>  * @param x a bencoded integer
<span class='line'> 26</span>  * @param f the current position in x
<span class='line'> 27</span>  * @throws ValueError: decoding int: found negative zero
<span class='line'> 28</span>  * @throws ValueError: decoding int: found leading zero
<span class='line'> 29</span>  * @return Array[int, size]: an array containing the integer and its size
<span class='line'> 30</span>  */</span><span class="WHIT">  
<span class='line'> 31</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">decode_int</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 32</span> </span><span class="WHIT">    </span><span class="COMM">// f is current index in string x.</span><span class="WHIT">
<span class='line'> 33</span> </span><span class="WHIT">    </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 34</span> </span><span class="WHIT">    </span><span class="COMM">// newf is the index of the end of the current integer in x</span><span class="WHIT">
<span class='line'> 35</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">newf</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">x.indexOf</span><span class="PUNC">(</span><span class="STRN">'e'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 36</span> </span><span class="WHIT">    </span><span class="COMM">// We're grabbing the stuff before e, and converting it to an integer</span><span class="WHIT">
<span class='line'> 37</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">n</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">parseInt</span><span class="PUNC">(</span><span class="NAME">x.substring</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">,</span><span class="NAME">newf</span><span class="PUNC">+</span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 38</span> </span><span class="WHIT">    </span><span class="COMM">// Handle negative</span><span class="WHIT">
<span class='line'> 39</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x.charAt</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">'-'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 40</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x.charAt</span><span class="PUNC">(</span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">'0'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 41</span> </span><span class="WHIT">               </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: decoding int: found negative zero"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 42</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 43</span> </span><span class="WHIT">    </span><span class="COMM">// Handle leading 0s</span><span class="WHIT">
<span class='line'> 44</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x.charAt</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">'0'</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="NAME">newf</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">+</span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 45</span> </span><span class="WHIT">           </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: decoding int: found leading zero"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 46</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 47</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="NAME">n</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">newf</span><span class="PUNC">+</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 48</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 49</span> 
<span class='line'> 50</span> </span><span class="COMM">/**
<span class='line'> 51</span>  * Decodes a bencoded string to an actual string
<span class='line'> 52</span>  * @param x a bencoded string
<span class='line'> 53</span>  * @param f the current position in x
<span class='line'> 54</span>  * @throws ValueError: decoding string: string length has leading 0 (or gap between 0 and colon)
<span class='line'> 55</span>  * @return Array[string, size]: an array containing the string and its size
<span class='line'> 56</span>  */</span><span class="WHIT">
<span class='line'> 57</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 58</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">colon</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">x.indexOf</span><span class="PUNC">(</span><span class="STRN">':'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 59</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">n</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">parseInt</span><span class="PUNC">(</span><span class="NAME">x.substring</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">,</span><span class="NAME">colon</span><span class="PUNC">+</span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 60</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x.charAt</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="STRN">'0'</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="NAME">colon</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">+</span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 61</span> </span><span class="WHIT">        </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: decoding string: string length has leading 0 (or gap between 0 and colon)"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 62</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 63</span> </span><span class="WHIT">    </span><span class="NAME">colon</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 64</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="NAME">x.substring</span><span class="PUNC">(</span><span class="NAME">colon</span><span class="PUNC">,</span><span class="NAME">colon</span><span class="PUNC">+</span><span class="NAME">n</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">colon</span><span class="PUNC">+</span><span class="NAME">n</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 65</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 66</span> 
<span class='line'> 67</span> </span><span class="COMM">/**
<span class='line'> 68</span>  * Decodes a bencoded list (i.e. an Array) into an actual list
<span class='line'> 69</span>  * @param x a bencoded list
<span class='line'> 70</span>  * @param f the current position in x
<span class='line'> 71</span>  * @return Array[list, size]: an array containing the list and its size
<span class='line'> 72</span>  */</span><span class="WHIT">
<span class='line'> 73</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">decode_list</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 74</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">r</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 75</span> </span><span class="WHIT">    </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">+</span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 76</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 77</span> </span><span class="WHIT">    </span><span class="KEYW">while</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x.charAt</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">'e'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 78</span> </span><span class="WHIT">        </span><span class="NAME">tmp</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">[</span><span class="NAME">f</span><span class="PUNC">]</span><span class="PUNC">]</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 79</span> </span><span class="WHIT">        </span><span class="NAME">v</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT"> 
<span class='line'> 80</span>         </span><span class="COMM">// NOTE: Push on Javascript array, append on Python array</span><span class="WHIT">
<span class='line'> 81</span> </span><span class="WHIT">        </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="NAME">v</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 82</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 83</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="NAME">r</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 84</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'> 85</span> 
<span class='line'> 86</span> </span><span class="COMM">/**
<span class='line'> 87</span>  * Decodes a bencoded dictionary (i.e. a hash table) into an actual dictionary
<span class='line'> 88</span>  * @param x a bencoded dictionary
<span class='line'> 89</span>  * @param f the current position in x
<span class='line'> 90</span>  * @throws ValueError: decoding dict: dictionary out of order (index N, Found KEY after LASTKEY)
<span class='line'> 91</span>  * @return Array[dict, size]: an array containing the dictionary and its size
<span class='line'> 92</span>  */</span><span class="WHIT">
<span class='line'> 93</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">decode_dict</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 94</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">r</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 95</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 96</span> </span><span class="WHIT">    </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">+</span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 97</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">lastkey</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 98</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 99</span> </span><span class="WHIT">    </span><span class="KEYW">while</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x.charAt</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">'e'</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>100</span> </span><span class="WHIT">        </span><span class="NAME">tmp</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>101</span> </span><span class="WHIT">        </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>102</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">lastkey</span><span class="WHIT"> </span><span class="PUNC">>=</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>103</span> </span><span class="WHIT">            </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: decoding dict: "</span><span class="WHIT">
<span class='line'>104</span> </span><span class="WHIT">                </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">"dictionary out of order (index "</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">", "</span><span class="WHIT">
<span class='line'>105</span> </span><span class="WHIT">                </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">"Found "</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">" after "</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">lastkey</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">")"</span><span class="WHIT">
<span class='line'>106</span> </span><span class="WHIT">            </span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>107</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>108</span> </span><span class="WHIT">        </span><span class="NAME">lastkey</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>109</span> </span><span class="WHIT">        </span><span class="NAME">tmp</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">[</span><span class="NAME">f</span><span class="PUNC">]</span><span class="PUNC">]</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>110</span> </span><span class="WHIT">        </span><span class="NAME">r</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>111</span> </span><span class="WHIT">        </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>112</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>113</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="NAME">r</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">f</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>114</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>115</span> 
<span class='line'>116</span> </span><span class="COMM">/**
<span class='line'>117</span>  * A hash table for determining which decode function to call
<span class='line'>118</span>  * &lt;br>&lt;p style="margin-left:1em">
<span class='line'>119</span>  * &lt;b> Keymap: &lt;/b>&lt;blockquote>
<span class='line'>120</span>  *
<span class='line'>121</span>  * 'l' => decode_list() &lt;br>
<span class='line'>122</span>  * 'd' => decode_dict() &lt;br>
<span class='line'>123</span>  * 'i' => decode_int() &lt;br>
<span class='line'>124</span>  * '0'-'9' => decode_string()
<span class='line'>125</span>  *
<span class='line'>126</span>  * &lt;/blockquote>&lt;/p>
<span class='line'>127</span>  */</span><span class="WHIT">
<span class='line'>128</span> </span><span class="NAME">decode_func</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>129</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'l'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_list</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>130</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'d'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_dict</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>131</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'i'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_int</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>132</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'0'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>133</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'1'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>134</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'2'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>135</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'3'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>136</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'4'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>137</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'5'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>138</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'6'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>139</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'7'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>140</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'8'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>141</span> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="STRN">'9'</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>142</span> 
<span class='line'>143</span> </span><span class="COMM">/**
<span class='line'>144</span>  * Decodes a bencoded variable
<span class='line'>145</span>  * @param x a bencoded value to decode
<span class='line'>146</span>  * @throws ValueError: not a string
<span class='line'>147</span>  * @throws ValueError: can't decode empty string
<span class='line'>148</span>  * @throws ValueError: unknown type, couldn't find decode function
<span class='line'>149</span>  * @throws ValueError: not a valid bencoded string
<span class='line'>150</span>  * @throws ValueError: invalid bencoded value (data after valid end (whitespace?))
<span class='line'>151</span>  * @return return value of the selected decode function
<span class='line'>152</span>  */</span><span class="WHIT">
<span class='line'>153</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">bdecode</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>154</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">l</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>155</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">r</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>156</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">e</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>157</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>158</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">bencode_typeof</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">"string"</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>159</span> </span><span class="WHIT">        </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: not a string"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>160</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>161</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">x.length</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>162</span> </span><span class="WHIT">        </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: can't decode empty string"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>163</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>164</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="KEYW">in</span><span class="WHIT"> </span><span class="NAME">decode_func</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>165</span> </span><span class="WHIT">        </span><span class="COMM">// couldn't find decode func</span><span class="WHIT">
<span class='line'>166</span> </span><span class="WHIT">        </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: unknown type, couldn't find decode function"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>167</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>168</span> </span><span class="WHIT">    </span><span class="NAME">tmp</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">decode_func</span><span class="PUNC">[</span><span class="NAME">x</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">]</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>169</span> </span><span class="WHIT">    </span><span class="NAME">r</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">[</span><span class="NUMB">0</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT"> 
<span class='line'>170</span>     </span><span class="NAME">l</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">tmp</span><span class="PUNC">[</span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>171</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">l</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="NAME">x.length</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>172</span> </span><span class="WHIT">        </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: invalid bencoded value (data after valid end (whitespace?))"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>173</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>174</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">r</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>175</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>176</span> 
<span class='line'>177</span> </span><span class="COMM">//from types import StringType, IntType, LongType, DictType, ListType, TupleType</span><span class="WHIT">
<span class='line'>178</span> 
<span class='line'>179</span> </span><span class="COMM">// We don't have Bencached working.</span><span class="WHIT">
<span class='line'>180</span> </span><span class="COMM">// The idea is (woah!) that you can cache the encoded value.</span><span class="WHIT">
<span class='line'>181</span> </span><span class="COMM">// Not actually required for bencoding, it's just a time saver.</span><span class="WHIT">
<span class='line'>182</span> 
<span class='line'>183</span> </span><span class="COMM">/**
<span class='line'>184</span>  * Caches a bencoded value
<span class='line'>185</span>  * @param s the bencoded value to cache
<span class='line'>186</span>  */</span><span class="WHIT">
<span class='line'>187</span> </span><span class="NAME">Bencached</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">s</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>188</span> </span><span class="WHIT">    </span><span class="NAME">this.bencoded</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">s</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>189</span> </span><span class="WHIT">    </span><span class="COMM">// TODO: can we enforece equivalent to __slots__ ?</span><span class="WHIT">
<span class='line'>190</span> </span><span class="WHIT">    </span><span class="COMM">// http://www.network-theory.co.uk/docs/pylang/__slots__.html</span><span class="WHIT">
<span class='line'>191</span> </span><span class="WHIT">    </span><span class="COMM">// __slots__ = ["bencoded"]</span><span class="WHIT">
<span class='line'>192</span> </span><span class="WHIT">    
<span class='line'>193</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>194</span> 
<span class='line'>195</span> </span><span class="COMM">/**
<span class='line'>196</span>  * Pushes a bencoded bencached value onto an array
<span class='line'>197</span>  * @param x an instance of Bencode
<span class='line'>198</span>  * @param r a reference to an Array
<span class='line'>199</span>  */</span><span class="WHIT">
<span class='line'>200</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">encode_bencached</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="NAME">r</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>201</span> </span><span class="WHIT">    </span><span class="COMM">// r is an array</span><span class="WHIT">
<span class='line'>202</span> </span><span class="WHIT">    </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="NAME">x.bencoded</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>203</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>204</span> 
<span class='line'>205</span> </span><span class="COMM">/**
<span class='line'>206</span>  * Pushes a bencoded integer onto an array
<span class='line'>207</span>  * @param x an instance of Bencode
<span class='line'>208</span>  * @param r a reference to an Array
<span class='line'>209</span>  */</span><span class="WHIT">
<span class='line'>210</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">encode_int</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">r</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>211</span> </span><span class="WHIT">    </span><span class="COMM">// r is an array</span><span class="WHIT">
<span class='line'>212</span> </span><span class="WHIT">    </span><span class="COMM">// here we add to the r array using push. Push takes n arguments, adds in order.</span><span class="WHIT">
<span class='line'>213</span> </span><span class="WHIT">    </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="STRN">"i"</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">"e"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>214</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>215</span> 
<span class='line'>216</span> </span><span class="COMM">/**
<span class='line'>217</span>  * Pushes a bencoded string onto an array
<span class='line'>218</span>  * @param x an instance of Bencode
<span class='line'>219</span>  * @param r a reference to an Array
<span class='line'>220</span>  */</span><span class="WHIT">
<span class='line'>221</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">encode_string</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">r</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>222</span> </span><span class="WHIT">    </span><span class="COMM">// r is an array</span><span class="WHIT">
<span class='line'>223</span> </span><span class="WHIT">    </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="NAME">x.length</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">':'</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT">
<span class='line'>224</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>225</span> 
<span class='line'>226</span> </span><span class="COMM">/**
<span class='line'>227</span>  * Pushes a bencoded list (i.e. an Array) onto an Array
<span class='line'>228</span>  * @param x an instance of Bencode
<span class='line'>229</span>  * @param r a reference to an Array
<span class='line'>230</span>  */</span><span class="WHIT">
<span class='line'>231</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">encode_list</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">r</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>232</span> </span><span class="WHIT">    </span><span class="COMM">// r is an array.</span><span class="WHIT">
<span class='line'>233</span> </span><span class="WHIT">    </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="STRN">'l'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT"> 
<span class='line'>234</span>     </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">x.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>235</span> </span><span class="WHIT">        </span><span class="COMM">// TODO: ensure typeof works</span><span class="WHIT">
<span class='line'>236</span> </span><span class="WHIT">        </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="NAME">bencode_typeof</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">]</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">r</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>237</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>238</span> </span><span class="WHIT">    </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="STRN">"e"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>239</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>240</span> 
<span class='line'>241</span> </span><span class="COMM">/**
<span class='line'>242</span>  * Pushes a bencoded dictionary (i.e. a Hash Table) onto an Array
<span class='line'>243</span>  * @param x an instance of Bencode
<span class='line'>244</span>  * @param r a reference to an Array
<span class='line'>245</span>  */</span><span class="WHIT">
<span class='line'>246</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">encode_dict</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="NAME">r</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>247</span> </span><span class="WHIT">    </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="STRN">"d"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>248</span> </span><span class="WHIT">    </span><span class="COMM">/*var ilist = x.items();
<span class='line'>249</span>     ilist.sort()
<span class='line'>250</span>     
<span class='line'>251</span>     for k, v in ilist:
<span class='line'>252</span>         r.extend((str(len(k)), ':', k))
<span class='line'>253</span>         encode_func[type(v)](v, r)
<span class='line'>254</span>     */</span><span class="WHIT">
<span class='line'>255</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>256</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">v</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>257</span> </span><span class="WHIT">    </span><span class="COMM">// This NEEDS to be in order.</span><span class="WHIT">
<span class='line'>258</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">l</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>259</span> </span><span class="WHIT">    </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">k</span><span class="WHIT"> </span><span class="KEYW">in</span><span class="WHIT"> </span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>260</span> </span><span class="WHIT">        </span><span class="NAME">l.push</span><span class="PUNC">(</span><span class="NAME">k</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>261</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>262</span> </span><span class="WHIT">    </span><span class="NAME">l.sort</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>263</span> </span><span class="WHIT">    </span><span class="COMM">// Now in order, we can create encoded form</span><span class="WHIT">
<span class='line'>264</span> </span><span class="WHIT">    </span><span class="KEYW">while</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">l.length</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>265</span> </span><span class="WHIT">        </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">l.shift</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>266</span> </span><span class="WHIT">        </span><span class="NAME">v</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">x</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>267</span> </span><span class="WHIT">        </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="NAME">k.length</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">""</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="STRN">":"</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>268</span> </span><span class="WHIT">        </span><span class="COMM">// TODO: ensure typeof works</span><span class="WHIT">
<span class='line'>269</span> </span><span class="WHIT">        </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="NAME">bencode_typeof</span><span class="PUNC">(</span><span class="NAME">v</span><span class="PUNC">)</span><span class="PUNC">]</span><span class="PUNC">(</span><span class="NAME">v</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">r</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">        
<span class='line'>270</span>     </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>271</span> </span><span class="WHIT">    </span><span class="NAME">r.push</span><span class="PUNC">(</span><span class="STRN">"e"</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>272</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>273</span> 
<span class='line'>274</span> </span><span class="COMM">// TODO: Note this is a new function to use instead of typeof(), as </span><span class="WHIT">
<span class='line'>275</span> </span><span class="COMM">//  typeof is causing probs</span><span class="WHIT">
<span class='line'>276</span> </span><span class="COMM">// Also note there is a duplicate of this function (excluding </span><span class="WHIT">
<span class='line'>277</span> </span><span class="COMM">//  Bencached) in generalmain of the bittorrent handler.</span><span class="WHIT">
<span class='line'>278</span> </span><span class="COMM">// This is because we need a handy function to get types, that draws</span><span class="WHIT">
<span class='line'>279</span> </span><span class="COMM">// a distinction between arrays and other objects.</span><span class="WHIT">
<span class='line'>280</span> 
<span class='line'>281</span> </span><span class="COMM">/**
<span class='line'>282</span>  * Returns the type of a bencoded object
<span class='line'>283</span>  * @param o a bencoded object
<span class='line'>284</span>  * @return The type of the object, can either be "array" or "bencached"
<span class='line'>285</span>  */</span><span class="WHIT">
<span class='line'>286</span> </span><span class="NAME">bencode_typeof</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">o</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>287</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">t</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">typeof</span><span class="PUNC">(</span><span class="NAME">o</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>288</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">t</span><span class="PUNC">==</span><span class="STRN">"object"</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>289</span> </span><span class="WHIT">        </span><span class="COMM">// We care about particular types</span><span class="WHIT">
<span class='line'>290</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">o</span><span class="WHIT"> </span><span class="KEYW">instanceof</span><span class="WHIT"> </span><span class="NAME">Array</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>291</span> </span><span class="WHIT">            </span><span class="NAME">t</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">"array"</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>292</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">o</span><span class="WHIT"> </span><span class="KEYW">instanceof</span><span class="WHIT"> </span><span class="NAME">Bencached</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>293</span> </span><span class="WHIT">            </span><span class="NAME">t</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="STRN">"bencached"</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>294</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>295</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>296</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">t</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>297</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>298</span> 
<span class='line'>299</span> </span><span class="COMM">/**
<span class='line'>300</span>  * A hash table for determining which encode function to call
<span class='line'>301</span>  * &lt;br>&lt;p style="margin-left:1em">
<span class='line'>302</span>  * &lt;b> Keymap: &lt;/b>&lt;blockquote>
<span class='line'>303</span>  *
<span class='line'>304</span>  * 'bencached' => encode_bencached() &lt;br>
<span class='line'>305</span>  * 'number' => encode_int() &lt;br>
<span class='line'>306</span>  * 'string' => encode_string() &lt;br>
<span class='line'>307</span>  * 'array' => encode_list() &lt;br>
<span class='line'>308</span>  * 'object' => encode_dict() &lt;br>
<span class='line'>309</span>  * 'boolean' => encode_int()
<span class='line'>310</span>  *
<span class='line'>311</span>  * &lt;/blockquote>&lt;/p>
<span class='line'>312</span>  */</span><span class="WHIT">
<span class='line'>313</span> </span><span class="NAME">encode_func</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>314</span> </span><span class="COMM">// TODO: handle Bencached somehow</span><span class="WHIT">
<span class='line'>315</span> </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="STRN">"bencached"</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">encode_bencached</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>316</span> </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="STRN">"number"</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">encode_int</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>317</span> </span><span class="COMM">// JS only has number type, don't draw a long/int distinction: encode_func[LongType] = encode_int</span><span class="WHIT">
<span class='line'>318</span> </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="STRN">"string"</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">encode_string</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>319</span> </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="STRN">"array"</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">encode_list</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>320</span> </span><span class="COMM">// JS has no tuples: encode_func[TupleType] = encode_list</span><span class="WHIT">
<span class='line'>321</span> </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="STRN">"object"</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">encode_dict</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>322</span> </span><span class="COMM">// TODO: Check encoding booleans as ints works. This is rare.</span><span class="WHIT">
<span class='line'>323</span> </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="STRN">"boolean"</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">encode_int</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>324</span> 
<span class='line'>325</span> </span><span class="COMM">/**
<span class='line'>326</span>  * Bencodes a value
<span class='line'>327</span>  * @param x the value to bencode
<span class='line'>328</span>  * @throws ValueError: bencode: no encoding function for type: type was &lt;bencode_typeof(x)>
<span class='line'>329</span>  * @return return value of the selected function
<span class='line'>330</span>  */</span><span class="WHIT">
<span class='line'>331</span> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">bencode</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>332</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">r</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>333</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">bencode_typeof</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="KEYW">in</span><span class="WHIT"> </span><span class="NAME">encode_func</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>334</span> </span><span class="WHIT">        </span><span class="NAME">encode_func</span><span class="PUNC">[</span><span class="NAME">bencode_typeof</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">]</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">r</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>335</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>336</span> </span><span class="WHIT">        </span><span class="KEYW">throw</span><span class="WHIT"> </span><span class="NAME">Components.Exception</span><span class="PUNC">(</span><span class="STRN">"ValueError: bencode: "</span><span class="WHIT">
<span class='line'>337</span> </span><span class="WHIT">            </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">"no encoding function for type: "</span><span class="WHIT">
<span class='line'>338</span> </span><span class="WHIT">            </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="STRN">"type was "</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">bencode_typeof</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>339</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>340</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">r.join</span><span class="PUNC">(</span><span class="STRN">""</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>341</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>342</span> 
<span class='line'>343</span> </span><span class="COMM">/**
<span class='line'>344</span>  * A class for bencoding and bdecoding data
<span class='line'>345</span>  * @constructor
<span class='line'>346</span>  */</span><span class="WHIT">
<span class='line'>347</span> </span><span class="NAME">Bencoding</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>348</span> 
<span class='line'>349</span> </span><span class="NAME">Bencoding.prototype</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>350</span> </span><span class="WHIT">    </span><span class="NAME">classDescription</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="STRN">"Bencoding"</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>351</span> </span><span class="WHIT">    </span><span class="NAME">contractID</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="STRN">"@wikiscraps.com/encoding/bencoding;1"</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>352</span> </span><span class="WHIT">    </span><span class="NAME">classID</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">Components.ID</span><span class="PUNC">(</span><span class="STRN">"d430638d-80b3-47ad-b9d9-5d94be758491"</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>353</span> </span><span class="WHIT">    </span><span class="NAME">QueryInterface</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">XPCOMUtils.generateQI</span><span class="PUNC">(</span><span class="WHIT">
<span class='line'>354</span> </span><span class="WHIT">        </span><span class="PUNC">[</span><span class="NAME">Components.interfaces.Bencoding</span><span class="WHIT">
<span class='line'>355</span> </span><span class="WHIT">        </span><span class="PUNC">,</span><span class="NAME">Components.interfaces.nsISupports</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>356</span> </span><span class="COMM">/**
<span class='line'>357</span>  * Bencodes some data
<span class='line'>358</span>  * @param data Some data to bencode
<span class='line'>359</span>  * @return the bencoded data
<span class='line'>360</span>  */</span><span class="WHIT">   
<span class='line'>361</span>     </span><span class="NAME">bencode</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">bencode</span><span class="PUNC">(</span><span class="NAME">data</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>362</span> 
<span class='line'>363</span> </span><span class="COMM">/**
<span class='line'>364</span>  * Bdecodes an object
<span class='line'>365</span>  * @param obj An object to bdecode
<span class='line'>366</span>  * @returns The bdecoded object
<span class='line'>367</span>  */</span><span class="WHIT">
<span class='line'>368</span> </span><span class="WHIT">    </span><span class="NAME">bdecode</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">obj</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">bdecode</span><span class="PUNC">(</span><span class="NAME">obj</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>369</span> </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>370</span> 
<span class='line'>371</span> </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">NSGetModule</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">XPCOMUtils.generateNSGetModule</span><span class="PUNC">(</span><span class="PUNC">[</span><span class="NAME">Bencoding</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>372</span> 
<span class='line'>373</span> </span></pre></body></html>